//
//  MLASavePinSupplierAddVC.m
//  MagicLampApp
//
//  Created by Zheng on 2018/12/24.
//  Copyright © 2018 zheng.magic.lamp.app. All rights reserved.
//

#import "MLASavePinSupplierAddVC.h"
#import "MLABaseFieldModel.h"
#import "MLABaseFieldView.h"
#import "MLASavePinCateVC.h"


@interface MLASavePinSupplierAddVC ()

@end

@implementation MLASavePinSupplierAddVC {
    NSArray<MLABaseFieldModel *> *modelsArr;
    MLABaseFieldView *cateFieldView;
    MLABaseFieldView *goodTypeView;
    
    NSString *userTypeId; //客户类型
    NSString *cateTypeId; //商品类型
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configTitle:@"添加"];
    
    [self addSubviewsInView];
    
    @weakify(self);
    cateFieldView.clickActionBlock = ^{
        @strongify(self);
        MLASavePinCateVC *cateVC = [[MLASavePinCateVC alloc] init];
        cateVC.type = @"1";
        cateVC.typeSelectBlock = ^(NSString * _Nonnull typeStr,NSString * _Nonnull typeId) {
            self->cateFieldView.inputText = typeStr;
            self->userTypeId = typeId;
        };
        [self.navigationController pushViewController:cateVC animated:YES];
    };
    
    goodTypeView.clickActionBlock = ^{
        @strongify(self);
        MLASavePinCateVC *cateVC = [[MLASavePinCateVC alloc] init];
        cateVC.type = @"3";
        cateVC.typeSelectBlock = ^(NSString * _Nonnull typeStr, NSString * _Nonnull typeId) {
            self->goodTypeView.inputText = typeStr;
            self->cateTypeId = typeId;
        };
        [self.navigationController pushViewController:cateVC animated:YES];
    };
    
}

#pragma mark - LoadData
- (void)loadDataWithParams:(NSDictionary *)params {
    [MLAHttpManage postHostType:MLAHttpHostType_Jinxiaocun api:kSupplierAdd params:params success:^(id  _Nonnull response, NSString * _Nonnull msg) {
        [MLAHudManage showMessage:@"添加成功"];
        if (self.addSuccessBlock) {
            self.addSuccessBlock();
        }
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^{
        
    }];
}

#pragma mark - Actions
- (void)commitBtnAction {
    CX_Log(@"添加采购商/供应商");
    
    NSString *name = [self getContentWithTag:14002];
    NSString *linkman = [self getContentWithTag:14003];
    NSString *phone = [self getContentWithTag:14004];
    
    if (!userTypeId) {
        [MLAHudManage showMessage:@"请选择客户类型"];
        return;
    }
    
    if (!cateTypeId) {
        [MLAHudManage showMessage:@"请选择商品类型"];
        return;
    }
    
    if (name.length == 0) {
        [MLAHudManage showMessage:@"请输入公司名称"];
        return;
    }
    
    if (linkman.length == 0) {
        [MLAHudManage showMessage:@"请输入联系人"];
        return;
    }
    
    if(phone.length != 11) {
        [MLAHudManage showMessage:@"请输入手机号"];
        return;
    }
    
    NSDictionary *params = @{@"phone":phone,
                             @"linkman":linkman,
                             @"name":name,
                             @"type_id":cateTypeId,
                             @"type":userTypeId};
    [self loadDataWithParams:params];
}

- (NSString *)getContentWithTag:(NSInteger)tag {
    MLABaseFieldView *fieldView = (MLABaseFieldView *)[self.tableView.tableHeaderView viewWithTag:tag];
    return fieldView.inputText;
}

#pragma mark - Subviews
- (void)addSubviewsInView {
    
    modelsArr = [self getModelsArr];
    
    CGFloat height = 550;
    
    UIView *tableHeaderView = [[UIView alloc] init];
    tableHeaderView.backgroundColor = CX_GetColorHex(0xFCFCFC, 1);
    tableHeaderView.frame = CGRectMake(0, 0, CX_GetScreenWidth(), height);
    
    [modelsArr enumerateObjectsUsingBlock:^(MLABaseFieldModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        MLABaseFieldView *fieldView = [[MLABaseFieldView alloc] init];
        fieldView.tag = 14000+idx;
        fieldView.model = obj;
        [tableHeaderView addSubview:fieldView];
        
        if (idx == 0) {
            self->cateFieldView = fieldView;
            [fieldView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(20);
                make.left.right.equalTo(tableHeaderView);
                make.height.mas_equalTo(85);
            }];
        } else {
            if (idx == 1) {
                self->goodTypeView = fieldView;
            }
            
            [fieldView mas_makeConstraints:^(MASConstraintMaker *make) {
                UIView *view = [tableHeaderView viewWithTag:14000+idx-1];
                make.left.right.height.equalTo(view);
                make.top.equalTo(view.mas_bottom);
            }];
        }
    }];
    
    UIButton *commitBtn = [self getCommitButtonWithTitle:@"添加"];
    [commitBtn addTarget:self action:@selector(commitBtnAction) forControlEvents:UIControlEventTouchUpInside];
    [tableHeaderView addSubview:commitBtn];
    [commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        UIView *view = [tableHeaderView viewWithTag:14000+self->modelsArr.count-1];
        make.top.equalTo(view.mas_bottom).offset(10);
        make.left.mas_equalTo(15);
        make.right.mas_equalTo(-15);
        make.height.mas_equalTo(40);
    }];
    
    self.tableView.tableHeaderView = tableHeaderView;
    [tableHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(0);
        make.width.mas_equalTo(CX_GetScreenWidth());
        make.height.mas_equalTo(height);
    }];
    
}

- (NSArray<MLABaseFieldModel *> *)getModelsArr {
    
    NSArray *infosArr = @[CX_GetFieldInfoDic(@"客户类型：", @"选择客户类型", @"30", @"0", @"0", @"1", @""),
                          CX_GetFieldInfoDic(@"商品类型：", @"选择商品类型", @"30", @"0", @"0", @"1", @""),
                          CX_GetFieldInfoDic(@"公司名称：", @"输入公司名称", @"30", @"0", @"1", @"0", @""),
                          CX_GetFieldInfoDic(@"联系人：", @"输入联系人", @"30", @"0", @"1", @"0", @""),
                          CX_GetFieldInfoDic(@"电话：", @"输入电话", @"11", @"0", @"1", @"0", @"")];
    return [[MLABaseFieldModel mj_objectArrayWithKeyValuesArray:infosArr] copy];
}




@end
